package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjy.dto.ContractDTO;
import com.ktjy.dto.LoginUser;
import com.ktjy.mapper.TbActivityMapper;
import com.ktjy.mapper.TbBusinessMapper;
import com.ktjy.mapper.TbContractMapper;
import com.ktjy.mapper.TbCourseMapper;
import com.ktjy.pojo.TbActivity;
import com.ktjy.pojo.TbBusiness;
import com.ktjy.pojo.TbContract;
import com.ktjy.pojo.TbCourse;
import com.ktjy.service.ITbContractService;
import com.ktjy.utils.Constants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 合同 服务实现类
 * </p>
 *
 * @author jjx
 * @since 2025-08-28
 */
@Service
public class TbContractServiceImpl extends ServiceImpl<TbContractMapper, TbContract> implements ITbContractService {
    @Autowired
    TbCourseMapper courseMapper;
    @Autowired
    TbActivityMapper activityMapper;
    @Autowired
    TbBusinessMapper businessMapper;
    @Autowired
    TbContractMapper contractMapper;

    @Override
    public void changeContract(TbContract tbContract) {
        //1.查询商机信息
        TbBusiness business = businessMapper.selectById(tbContract.getBusinessId());
        if (business.getCourseId() == null) {
            throw new RuntimeException("商机里面没有选择课程，无法转换客户合同");
        }
        //2.修改商机状态
        business.setStatus(Constants.TOCUSTOMER);
        businessMapper.updateById(business);
        //3.保存合同记录
        BeanUtils.copyProperties(business, tbContract);
        this.saveContract(tbContract);
    }

    @Override
    public void saveContract(TbContract contract) {
        LoginUser loginUser=(LoginUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();
        String businessKey = "" + System.nanoTime();
        contract.setId(businessKey);
        contract.setCreateBy(null);
        contract.setCreateTime(null);
        contract.setUpdateBy(null);
        contract.setUpdateTime(null);
        contract.setDeptId(loginUser.getDeptId());
        contract.setStatus(Constants.STATE_FOUR+"");//状态(待审核1，已通过2，已驳回3 全部完成4)
        //计算合同价格
        calculateContractprice(contract);
        this.save(contract);
    }

    @Override
    public void updateContract(TbContract contract) {
        contract.setStatus(Constants.STATE_TWO+"");
        //计算合同价格
        calculateContractprice(contract);
        this.updateById(contract);
    }

    @Override
    public IPage<TbContract> findPage(ContractDTO contractDTO, Integer pageNo, Integer pageSize) {
        //构造分页条件
        Page page=new Page(pageNo,pageSize);
        return contractMapper.getPage(page,contractDTO);
    }

    /**
     * 计算合同价格
     * @param contract
     */
    private void calculateContractprice(TbContract contract) {
        TbCourse course = courseMapper.selectById(contract.getCourseId());
        contract.setCoursePrice(course.getPrice());

        TbActivity activity = activityMapper.selectById(contract.getActivityId());
        if (activity != null) {
            if ("1".equals(activity.getType())) {
                contract.setDiscountType("课程折扣");
                contract.setContractOrder(course.getPrice()*activity.getDiscount());
            } else {
                //当代金券大于等于合同50% 按照原价
                if (activity.getVouchers() >= course.getPrice() / 2) {
                    contract.setContractOrder(course.getPrice());
                    contract.setDiscountType("代金券大于原课程50%,代金券不可用");
                } else {
                    contract.setDiscountType("代金券");
                    contract.setContractOrder(course.getPrice() - activity.getVouchers());
                }
            }
        } else {
            contract.setDiscountType("无");
            contract.setCoursePrice(course.getPrice());
        }
    }
}
